package com.ala4.oxcafe.boot.utils;

import com.ala4.oxcafe.common.utils.id.IdUtil;
import org.slf4j.MDC;

/**
 * MDC 日志ID
 *
 * @author PING
 * @date 2024/1/18 22:32
 */
public final class ThreadMdcUtil {

    public static final String TRACE_ID = "TRACE_ID";
    public static final String HEADER_TRACE_ID = "X-Trace-Id";

    private ThreadMdcUtil() {
    }

    public static void setTraceIdIfAbsent() {
        if (MDC.get(TRACE_ID) == null) {
            MDC.put(TRACE_ID, IdUtil.objectId());
        }
    }

    public static void remove() {
        MDC.remove(TRACE_ID);
    }

    public static void remove(String key) {
        MDC.remove(key);
    }

    public static String getTraceId() {
        return MDC.get(TRACE_ID);
    }

    public static void setTraceIdIfAbsent(String headerTraceId) {
        MDC.put(TRACE_ID, headerTraceId);
    }
}
